package com.wgj.android.dialog;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.imageload.XImage;
import com.wgj.android.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/2/2.
 */

public class PictureDialog extends AppCompatDialogFragment {
    @BindView(R.id.viewpage)
    ViewPager viewpage;
    @BindView(R.id.textView)
    TextView textView;

    private String imgs;
    private String img[];
    private String title[];
    private List<ImageView> imageViews = new ArrayList<>();

    public static PictureDialog getInstael(String imgs) {
        PictureDialog qdDialog = new PictureDialog();
        Bundle bundle = new Bundle();
        //  bundle.putBoolean("isOk", isOk);
        bundle.putString("imgs", imgs);
        // bundle.putSerializable("advers", Adver);
        qdDialog.setArguments(bundle);
        return qdDialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imgs = getArguments().getString("imgs");
        try {
            //imgs.replace("""","");
            JSONArray array = new JSONArray(imgs);
            for (int i = 0; i < array.length(); i++) {
                img = new String[array.length()];
                title = new String[array.length()];
                JSONObject object = array.getJSONObject(i);
                img[i] = object.getString("img");
                title[i] = object.getString("title");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog_setting);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.picture_dialog, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        for (int i = 0; i < img.length; i++) {
            ImageView image = new ImageView(getContext());
            XImage.getInstance().load(image, img[i]);
            imageViews.add(image);
        }
        viewpage.setAdapter(new MyPagerAdapter());

        viewpage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面改变的时候调用(稳定),positon表示被选中的索引
            @Override
            public void onPageSelected(int position) {
                textView.setText(title[position]);//设置文字描述内容
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
            }

            @Override
            //页面滚动状态发送改变的时候回调
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViews.get(position));
            return imageViews.get(position);
        }

        /**
         * 销毁一个子条目,object就为instantiateItem方法创建的返回的对象,也是滑出去需要销毁了的视图对象
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
            object = null;
            System.out.println("销毁一个视图");
        }


    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(true); // 外部点击取消
        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.height = XDensityUtils.getScreenHeight();
        lp.width = XDensityUtils.getScreenWidth();
        window.setAttributes(lp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }


}
